<template>
  <div class="qr-code">
    <my-head :link="link" :title="title"></my-head>
    <div style="background: black;position: fixed;top: 1.6rem;left: 0;right: 0;bottom: 0;">
      <div id="bcid"></div>
    </div>
  </div>
</template>

<script>
  import MyHead from '../help/MyHead'
  let scan = null;

  export default {
    data() {
      return {
        link: '-1',
        title: '扫描',
        codeUrl: '',
      }
    },
    components: { MyHead},
    created(){

    },
    activated() {
      //	this.startRecognize();
    },
    mounted(){
      setTimeout(() => {
        this.startRecognize();
      },500)
    },
    methods: {
      startRecognize() {
        let _this = this;
        if (!window.plus) return;
        //创建扫描控件
        scan = new plus.barcode.Barcode('bcid',
          [plus.barcode.QR, plus.barcode.EAN8, plus.barcode.EAN13],
          { frameColor: "#00FF00", scanbarColor: "#00FF00" });
        //开始扫描
        scan.start();
        scan.onmarked = onmarked;
        function onmarked(type, result, file) {
          let codes = result.split(":");
          let key = codes[0];
          let value = codes[1];
          switch (type) {
            case plus.barcode.QR:
              type = 'QR';
              break;
            case plus.barcode.EAN13:
              type = 'EAN13';
              break;
            case plus.barcode.EAN8:
              type = 'EAN8';
              break;
            default:
              _this.$layer.open({time:3,skin:'msg',content:'未能识别二维码'});
              scan.start();
              break;
          }

          result = result.replace(/\n/g, '')
          _this.codeUrl = result//扫描后返回值
          _this.$store.commit('codevalue', result)
          scan.cancel()//关闭扫描
          scan.close()//关闭条码识别控件
          _this.$router.go(-1)
          // if (_this.codeUrl) {
          //   _this.isShow = true
          // }


          // result = result.replace(/\n/g, '');
          // _this.codeUrl = result;
          // if(_this.$route.params.type == 'homeIndex'){
          // 	_this.$router.push('/home')
          // }else if(_this.$route.params.type == 'transferAccounts'){
          // 	_this.codeResult.$emit('result',result);
          // 	_this.$router.push('/transferAccounts')
          // }
        }
      }
    },
    beforeDestroy() {
      if(scan){
        //关闭扫描控件
        scan.close();
      }
    },
    computed:{

    }
  }
</script>

<style lang="css">
  .qr-code{
    background-color: #FAF8F5;
  }
  #bcid {
    width: 100%;
    position: absolute;
    left: 0;
    right: 0;
    top: 1.6rem;
    bottom:3rem;
    text-align: center;
    color: #fff;
    background: #000;
  }
</style>
